package com.yuju.controller.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.order.enums.RoomOrderChangeReturnCodeEnum;
import com.yuju.order.model.RoomOrderChangeAccountModel;
import com.yuju.order.model.RoomOrderChangeModel;
import com.yuju.order.service.IRoomOrderChangeService;
import com.yuju.util.Utils;
import com.yuju.vo.order.RoomOrderChangeVO;


@Controller
@RequestMapping("roomOrderChange")
public class RoomOrderChangeController extends BaseController<RoomOrderChangeVO, RoomOrderChangeModel> {

	@Autowired
	private IRoomOrderChangeService roomOrderChangeService;
	/**
	 * 
	 * 功能：改单列表<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月3日 下午2:49:14 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		RoomOrderChangeVO roomOrderChangeVO = obj.toJavaObject(RoomOrderChangeVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomOrderChangeVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = roomOrderChangeService.changeOrderList(convert(roomOrderChangeVO), roomOrderChangeVO.getPage(), roomOrderChangeVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：新增改单<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月3日 下午2:49:30 <br/>
	 */
	
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		RoomOrderChangeVO roomOrderChangeVO = obj.toJavaObject(RoomOrderChangeVO.class);
		roomOrderChangeVO.setCreateEmpId(roomOrderChangeVO.getEmpId());
		JSONArray jsonArray = obj.getJSONArray("roomOrderChangeAccountModels");
		//将array数组转换成字符串  
		String js = jsonArray.toJSONString();
		//把字符串转换成集合  
		List<RoomOrderChangeAccountModel> roomOrderChangeAccountModels = (List<RoomOrderChangeAccountModel>) JSONObject.parseObject(js,  new TypeReference<ArrayList<RoomOrderChangeAccountModel>>() {});
		
		Result result = new Result();
		int rows = roomOrderChangeService.saveRoomOrderChange(convert(roomOrderChangeVO),roomOrderChangeAccountModels);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：修改改单<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月3日 下午2:49:40 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		RoomOrderChangeVO roomOrderChangeVO = obj.toJavaObject(RoomOrderChangeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomOrderChangeVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = roomOrderChangeService.updateSelective(convert(roomOrderChangeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：删除改单<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月3日 下午2:49:48 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		RoomOrderChangeVO roomOrderChangeVO = obj.toJavaObject(RoomOrderChangeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomOrderChangeVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = roomOrderChangeService.del(roomOrderChangeVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_DEL_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_DEL_FAILURE.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		RoomOrderChangeVO roomOrderChangeVO = obj.toJavaObject(RoomOrderChangeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomOrderChangeVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		RoomOrderChangeModel RoomOrderChangeModel = roomOrderChangeService.findById(roomOrderChangeVO.getId());
		if (Utils.isNotNull(RoomOrderChangeModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(RoomOrderChangeModel);
			result.setReturnCode(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomOrderChangeReturnCodeEnum.ROOMORDERCHANGE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}

}
